package com.bcu.alumnus.service;

import com.bcu.alumnus.entity.Knowledge;
import com.bcu.alumnus.entity.Message;
import com.bcu.alumnus.repo.KnowledgeRespository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Validated
@Transactional
public class KnowledgeService {



    private Logger logger = LoggerFactory.getLogger(getClass());


    @Resource
    private KnowledgeRespository knowledgeRespository;


    /**
     * @Author: Gzz
     * @Date: 2020/9/28 下午5:00
     * @Description: 分享知识
     */
    public Message<Knowledge> addKnowledge(Knowledge knowledge){
        logger.info("开始 分享知识 方法");
        knowledge.setKnowledgeTime(new Date());
        Knowledge knowledgeRs = knowledgeRespository.save(knowledge);
        return Message.success(null).add(knowledgeRs);
    }


    /**
     * @Author: Gzz
     * @Date: 2020/9/28 下午5:04
     * @Description: 根据知识编号查找知识
     */
    public Message<Knowledge> getKnowledgeByKnowledgeId(int knowledgeId){
        logger.info("开始 根据知识编号查找知识 方法");
        Knowledge knowledgeRs = knowledgeRespository.getKnowledgeByKnowledgeId(knowledgeId);
        if(knowledgeRs != null){
            return Message.success(null).add(knowledgeRs);
        }
        return Message.fail("未查找到指定知识信息");
    }


    /**
     * @Author: Gzz
     * @Date: 2020/9/28 下午5:06
     * @Description: 根据知识编号，删除知识信息
     */
    public Message deleteKnowledgeByKnowledgeId(int knowledgeId){
        logger.info("开始 根据知识编号，删除知识信息 方法");
        int Rs = knowledgeRespository.deleteKnowledgeByKnowledgeId(knowledgeId);
        if (Rs == 1) {
            return Message.success(null);
        }
        return Message.fail(null);
    }


    /**
     * @Author: Gzz
     * @Date: 2020/9/28 下午5:26
     * @Description: 获取全部知识信息
     */
    public Message<List<Knowledge>> getKnowledges(){
        logger.info("开始 获取全部知识信息 方法");
        List<Knowledge> knowledgeList = knowledgeRespository.findAll();
        return Message.success(null).add(knowledgeList);
    }


    /**
     * @Author: Gzz
     * @Date: 2020/9/28 下午5:32
     * @Description: 根据发布者编号，查找知识信息
     */
    public Message<List<Knowledge>> getKnowledgeByKnowledgeUserId(String knowledgeUserId){
        logger.info("开始 根据发布者编号，查找知识信息 方法");
        List<Knowledge> knowledgeList = knowledgeRespository.getKnowledgesByKnowledgeUserId(knowledgeUserId);
        return Message.success(null).add(knowledgeList);
    }


    /**
     * @Author: Gzz
     * @Date: 2020/9/28 下午5:36
     * @Description: 根据学部编号，查找知识信息
     */
    public Message<List<Knowledge>> getKnowledgesByKnowledgePartId(int partId){
        logger.info("开始 根据学部编号，查找知识信息 方法");
        List<Knowledge> knowledgeList = knowledgeRespository.getKnowledgesByKnowledgePartId(partId);
        return Message.success(null).add(knowledgeList);
    }

}
